package com.dunqi.gpm.shunqing.home.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.api.ApiFactory;
import com.dunqi.gpm.shunqing.common.api.RxSchedulers;
import com.dunqi.gpm.shunqing.common.base.BaseFragment;
import com.dunqi.gpm.shunqing.common.base.RecyclerAdapter;
import com.dunqi.gpm.shunqing.common.util.SwipeUtil;
import com.dunqi.gpm.shunqing.home.bean.EarlyInfo;
import com.dunqi.gpm.shunqing.home.bean.Page;
import com.dunqi.gpm.shunqing.home.constance.Constance;
import com.dunqi.gpm.shunqing.home.widget.EmptyView;
import com.dunqi.gpm.shunqing.home.widget.NormalView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import io.reactivex.functions.Consumer;

/**
 * Created by 空 on 2018/1/5 0005.
 */

public class AuditEarlyInfoFragment extends BaseFragment implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    List<EarlyInfo> data = new ArrayList();

    HashMap<String, String> parms = new HashMap<>();
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;
    private long projectId;
    private long proAnnualPlanId;
    private AuditEarlyInfoAdapter adapter;


    @Override
    protected void refreshData(Bundle savedInstanceState) {

        net();
    }

    private void net() {
        parms.put(Constance.PROJECT_ID, String.valueOf(projectId));
        parms.put(Constance.PAGE, String.valueOf(page));

        parms.put("pro_annual_plan_id", String.valueOf(proAnnualPlanId));

        ApiFactory.getInstance()
                .queryProjectPreStageList(parms)
                .compose(RxSchedulers.<Page<EarlyInfo>>io_main())
                .subscribe(new Consumer<Page<EarlyInfo>>() {
                    @Override
                    public void accept(Page<EarlyInfo> earlyInfoPage) throws Exception {
                        SwipeUtil.loadCompleted(swipeRefresh);

                        ArrayList<EarlyInfo> list = earlyInfoPage.getList();

                        if (page == 1) {
                            data.clear();
                        }

                        data.addAll(list);
                        page++;
                        adapter.notifyDataSetChanged();

                        if (earlyInfoPage.isHasNextPage()) {
                            adapter.loadMoreComplete();
                        } else {
                            adapter.loadMoreEnd();
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        adapter.loadMoreFail();
                    }
                });
    }

    @Override
    protected void initUiAndListener() {

        recycler.setLayoutManager(new LinearLayoutManager(context));
        adapter = new AuditEarlyInfoAdapter(data);
        adapter.setEmptyView(new EmptyView(context));
        recycler.setAdapter(adapter);

        adapter.setOnLoadMoreListener(this, recycler);

        swipeRefresh.setOnRefreshListener(this);


    }

    @Override
    public void onLoadMoreRequested() {
        net();
    }

    @Override
    public void onRefresh() {
        page = 1;
        net();
    }


    class AuditEarlyInfoAdapter extends RecyclerAdapter<EarlyInfo> {
        public AuditEarlyInfoAdapter(@Nullable List<EarlyInfo> data) {
            super(R.layout.fragment_audit_early_info_, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, EarlyInfo item) {
            NormalView stateNameView = helper.getView(R.id.state_name_view);
            stateNameView.setText(item.getStageName());
            NormalView attachBranchView = helper.getView(R.id.attach_branch_view);
            attachBranchView.setText(item.getRespOrgName());
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();

        projectId = arguments.getLong(Constance.PROJECT_ID);
        proAnnualPlanId = arguments.getLong(Constance.YEAR);
    }


    public static AuditEarlyInfoFragment newInstance(long proAnnualPlanId, long projectId) {
        AuditEarlyInfoFragment fragment = new AuditEarlyInfoFragment();
        Bundle bundle = new Bundle();
        bundle.putLong(Constance.PROJECT_ID, projectId);
        bundle.putLong(Constance.YEAR, proAnnualPlanId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_audit_early_info;
    }

}
